package com.bobi.tfca.ui.mainnavigation.message.letter

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.databinding.FragmentLetterBinding
import com.bobi.tfca.network.response.MessageResponse
import com.bobi.tfca.ui.adapter.LetterAndNotificationAdapter

class LetterFragment : BaseFragment<FragmentLetterBinding, LetterFragmentVM>(
    FragmentLetterBinding::inflate,
    LetterFragmentVM::class.java,
    true
) {
    // 聊天室RV
    private lateinit var letterRV: RecyclerView
    private lateinit var noDataIV: ImageView

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentLetterBinding,
        viewModel: LetterFragmentVM?,
        savedInstanceState: Bundle?
    ) {
        // 设置头部菜单的按钮
        setTopPanelStyle(binding)

        // 信件RecyclerView初始化
        initRecyclerView(binding)

        noDataIV = binding.ivNoData
        // 设置监听
        viewModel!!.apply {
            letters.observe(this@LetterFragment) {
                val adapter = letterRV.adapter as LetterAndNotificationAdapter
                adapter.setItemList(it)
                adapter.notifyDataSetChanged()
            }
        }
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentLetterBinding) {
        val topPanel = binding.root.findViewById<View>(R.id.topPanel)
        //左边布局
        val leftLayout = topPanel.findViewById<LinearLayout>(R.id.left_layout)
        leftLayout.setOnClickListener{
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }

        // 右边布局
        val rightLayout = topPanel.findViewById<LinearLayout>(R.id.right_layout)
        val rightButton = rightLayout.findViewById<Button>(R.id.right_button)
        rightButton.text = "写信"
        rightButton.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_letterFragment_to_writeLetterFragment)
        }

        // 中间标题
        val title = topPanel.findViewById<TextView>(R.id.title_text)
        title.text = "信件"
    }


    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentLetterBinding
    ) {
        // 获取聊天室RecyclerView实例
        letterRV = binding.letterRV

        // 获得所有聊天室
        val letterList : ArrayList<MessageResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        letterRV.layoutManager = linearLayoutManager
        letterRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        var chatRoomAdapter = LetterAndNotificationAdapter(this, letterList)

        // 设置群聊天室监听器
        chatRoomAdapter.setOnItemClickListener(object : LetterAndNotificationAdapter.OnItemClickListener {
            override fun onItemClick(position: Int) {
                // 根据Int 跳转并设置对应的Fragment，携带参数
                val letter = chatRoomAdapter.list[position]
                publicViewModel!!.message.value = letter
                requireActivity().findNavController(R.id.app_nav).navigate(R.id.action_letterFragment_to_letterDetailFragment)
            }
        })
        // 绑定Adapter
        letterRV.adapter = chatRoomAdapter
    }

    override fun resumeFragment(viewModel: LetterFragmentVM?) {
        super.resumeFragment(viewModel)
        viewModel!!.apply {
            findAllLetters(publicViewModel!!, this@LetterFragment, noDataIV)
        }
    }
}